package com.castle.fortress.admin.study.exam.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.study.exam.entity.StudyExamQuestionEntity;
import com.castle.fortress.admin.study.exam.dto.StudyExamQuestionDto;
import com.castle.fortress.admin.study.exam.mapper.StudyExamQuestionMapper;
import com.castle.fortress.admin.study.exam.service.StudyExamQuestionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 试卷题目管理 服务实现类
 *
 * @author castle
 * @since 2023-02-14
 */
@Service
public class StudyExamQuestionServiceImpl extends ServiceImpl<StudyExamQuestionMapper, StudyExamQuestionEntity> implements StudyExamQuestionService {
    /**
    * 获取查询条件
    * @param studyExamQuestionDto
    * @return
    */
    public QueryWrapper<StudyExamQuestionEntity> getWrapper(StudyExamQuestionDto studyExamQuestionDto){
        QueryWrapper<StudyExamQuestionEntity> wrapper= new QueryWrapper();
        if(studyExamQuestionDto != null){
            StudyExamQuestionEntity studyExamQuestionEntity = ConvertUtil.transformObj(studyExamQuestionDto,StudyExamQuestionEntity.class);
            wrapper.like(studyExamQuestionEntity.getId() != null,"id",studyExamQuestionEntity.getId());
            wrapper.like(studyExamQuestionEntity.getExamId() != null,"exam_id",studyExamQuestionEntity.getExamId());
            wrapper.like(studyExamQuestionEntity.getQbId() != null,"qb_id",studyExamQuestionEntity.getQbId());
            wrapper.like(studyExamQuestionEntity.getQuestionId() != null,"question_id",studyExamQuestionEntity.getQuestionId());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getQuestionType()),"question_type",studyExamQuestionEntity.getQuestionType());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getTitle()),"title",studyExamQuestionEntity.getTitle());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getTitleImg()),"title_img",studyExamQuestionEntity.getTitleImg());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getTitleMp3()),"title_mp3",studyExamQuestionEntity.getTitleMp3());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getOptionType()),"option_type",studyExamQuestionEntity.getOptionType());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnalysis()),"analysis",studyExamQuestionEntity.getAnalysis());
            wrapper.like(studyExamQuestionEntity.getSort() != null,"sort",studyExamQuestionEntity.getSort());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswer()),"answer",studyExamQuestionEntity.getAnswer());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerA()),"answer_a",studyExamQuestionEntity.getAnswerA());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerB()),"answer_b",studyExamQuestionEntity.getAnswerB());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerC()),"answer_c",studyExamQuestionEntity.getAnswerC());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerD()),"answer_d",studyExamQuestionEntity.getAnswerD());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerE()),"answer_e",studyExamQuestionEntity.getAnswerE());
            wrapper.like(StrUtil.isNotEmpty(studyExamQuestionEntity.getAnswerF()),"answer_f",studyExamQuestionEntity.getAnswerF());
        }
        return wrapper;
    }


    @Override
    public IPage<StudyExamQuestionDto> pageStudyExamQuestion(Page<StudyExamQuestionDto> page, StudyExamQuestionDto studyExamQuestionDto) {
        QueryWrapper<StudyExamQuestionEntity> wrapper = getWrapper(studyExamQuestionDto);
        Page<StudyExamQuestionEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<StudyExamQuestionEntity> studyExamQuestionPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<StudyExamQuestionDto> pageDto = new Page(studyExamQuestionPage.getCurrent(), studyExamQuestionPage.getSize(),studyExamQuestionPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyExamQuestionPage.getRecords(),StudyExamQuestionDto.class));
        return pageDto;
    }


    @Override
    public List<StudyExamQuestionDto> listStudyExamQuestion(StudyExamQuestionDto studyExamQuestionDto){
        QueryWrapper<StudyExamQuestionEntity> wrapper = getWrapper(studyExamQuestionDto);
        List<StudyExamQuestionEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,StudyExamQuestionDto.class);
    }

    @Override
    public void deleteByExamId(Long examId) {
        QueryWrapper<StudyExamQuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id",examId);
        baseMapper.delete(wrapper);
    }
}

